package liuyuyang.controller;

import liuyuyang.domain.User;
import liuyuyang.domain.UserRole;
import liuyuyang.pojo.Result;
import liuyuyang.service.UserRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/userRole")
public class UserRoleController {
    @Resource
    private UserRoleService userRoleService;

    @PostMapping("/{uid}")
    public Result<String> auth(@PathVariable Integer uid, @RequestBody List<Integer> uids) {
        try {
            userRoleService.auth(uid, uids);

            return Result.success("授权成功");
        } catch (Exception e) {
            return Result.error("授权失败");
        }
    }

    @GetMapping
    public Result<List<UserRole>> list() {
        return Result.success(userRoleService.list());
    }

    @GetMapping("/user/{rid}")
    public Result<List<User>> getUserRoleList(@PathVariable Integer rid) {
        return Result.success(userRoleService.getUserRoleList(rid));
    }

    // 绑定权限
    @PostMapping("/user/{rid}")
    public Result<String> binding(@PathVariable Integer rid, @RequestBody List<Integer> uids) {
        try {
            userRoleService.binding(rid, uids);
            return Result.success("绑定成功");
        } catch (Exception e) {
            return Result.success("绑定失败" + e.getMessage());
        }
    }
}
